
import pymongo

src_client = pymongo.MongoClient("mongodb://172.16.20.70:27017")
tgt_client = pymongo.MongoClient("mongodb://root:!(%2FQqi%2B9ka0y@192.168.200.108:27017/?authSource=admin&replicaSet=rs0")

src_db = src_client["bms_test2"]
tgt_db = tgt_client["bms_prd"]

src_sys = src_db["SystemBusinessDataType"]
tgt_sys = tgt_db["SystemBusinessDataType"]

src_bmspar = src_db["BmsParameters"]
tgt_bmspar = tgt_db["BmsParameters"]

src_sys_data = src_sys.find({})

for item in src_sys_data:
    if not item["Name"] in ["OMS阿斯利康出库明细"]:
        continue
    id = item["_id"]
    print("同步-{}".format(item["Name"]))
    findOne = tgt_sys.find_one({"_id": id})

    if findOne == None:
        tgt_sys.insert_one(item)
    else:
        del item["_id"]
        # print(id)
        tgt_sys.update_one({"_id": id}, { "$set": item })

    pars = item["Parameters"]
    for parid in pars:
        src_par_item = src_bmspar.find_one({"_id": parid})
        tgt_par_item = tgt_bmspar.find_one({"_id": parid})
        if src_par_item == None:
            continue
        if tgt_par_item == None:            
            tgt_bmspar.insert_one(src_par_item)
        else:
            del src_par_item["_id"]
            tgt_bmspar.update_one({"_id": parid}, { "$set": src_par_item })
